package xjh.him.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.xjh.him.R;

import java.util.List;

import xjh.him.adapter.FriendAdapter;
import xjh.him.common.SharedData;
import xjh.him.entity.User;
import xjh.him.netty.codec.Packet;
import xjh.him.netty.dto.FriendDto;
import xjh.him.util.LogcatUtil;


public class HomeFriendActivity extends Activity {
    private static FriendAdapter adapter;
    private static List<User> beans;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_friend);
    }

    public static void initView(View view, Activity activity) {
        TextView countBubbleText = view.findViewById(R.id.new_friend_count_bubble);
        SharedData.countBubbleText = countBubbleText;

        FriendDto.SelectCountApplyReq req = new FriendDto.SelectCountApplyReq();
        req.setMeId(SharedData.me.getId());
        SharedData.client.sendMsg(Packet.create(FriendDto.SelectCountApplyReq.class, req));
        Object obj = SharedData.future.get();
        if (obj instanceof FriendDto.SelectCountApplyResp) {
            FriendDto.SelectCountApplyResp resp = (FriendDto.SelectCountApplyResp) obj;
            LogcatUtil.d("apply count: %d", resp.getCount());
            countBubbleText.setText(String.valueOf(resp.getCount()));
            if (resp.getCount() == 0)
                countBubbleText.setVisibility(View.INVISIBLE);
        }

        setAdapter(view, activity);
        onNewFriendBtnClick(view, activity);
    }

    public static void setAdapter(View view, Activity activity) {
        if (SharedData.friendBeans != null) {
            beans = SharedData.friendBeans;
        }else {
            FriendDto.SelectFriendsReq req = new FriendDto.SelectFriendsReq();
            req.setUserId(SharedData.me.getId());
            SharedData.client.sendMsg(Packet.create(FriendDto.SelectFriendsReq.class, req));
            Object obj = SharedData.future.get();
            if (obj instanceof FriendDto.SelectFriendsResp) {
                FriendDto.SelectFriendsResp resp = (FriendDto.SelectFriendsResp) obj;
                beans = resp.getUserList();
            }
        }

        ListView friendList = view.findViewById(R.id.friend_list);
        FriendAdapter adapter = new FriendAdapter(beans, R.layout.home_item);
        friendList.setAdapter(adapter);
        friendList.setOnItemClickListener((adapterView, view12, i, l) -> {
            Intent userInfoIntent = new Intent(activity, UserInfoActivity.class);
            userInfoIntent.putExtra("chatter", beans.get(i));
            activity.startActivityForResult(userInfoIntent, SharedData.REQ_CODE_USER_INFO);
        });
    }

    public static void onNewFriendBtnClick(View view, Activity activity) {
        RelativeLayout layout = view.findViewById(R.id.new_friend_layout);
        layout.setOnClickListener((v) -> {
            Intent intent = new Intent(activity, NewFriendActivity.class);
            activity.startActivity(intent);
        });
    }

    public static FriendAdapter getAdapter() {
        return adapter;
    }
}
